<?php
class WjbinviteAction extends UserAction{


    public function _initialize(){
        parent::_initialize();
        $this->canUseFunction('WjbInvite');
    }

    //用户邀请函编辑首页
    public function index(){
        $token = $_GET['token'];
        $res = M('Wjbinvite_data')->where(array('token'=>$token))->Order('id desc')->select();
        $this->assign('res',$res);
        $this->display();
    }

    public function add(){
        $r = M('Wjbinvite_model')->select();
        $this->assign('modellist',$r);
        //var_dump($r);
        $this->display();
    }


    public function getmodel(){
        $id = $_GET['id'];

        $model = M("Wjbinvite_model")->where(array('id'=>$id))->find();

        eval($model['tpl']);

        $config = unserialize($model['config']);

        $configInfo = unserialize($model['configinfo']);

        $info = array();

        $info['config'] = $config;

        $info['configInfo'] = $configInfo;

        $info = json_encode($info);

        echo $info;
    }

    public function addpage(){
        $id = $_GET['id'];
        $pagetype = $_GET['pagetype'];
        $token = $_GET['token'];
        $model = M('Wjbinvite_model')->where(array('id'=>$id))->find();
        $config = unserialize($model['config']);
        $pageConfig = $config[$pagetype];
        $configInfo = unserialize($model['configinfo']);

        //生成表单
        $form = '';//用ajax 还是用刷新页面的方式  如果刷新的方式 id是个问题 用ajax就需要一个页面一个页面添加 或者加个数字标签
        // 在这里只能生成一个统一的 要区分就要到前端页面区分
        //或者前台传进来数字 告诉当前是第几个 这样也行 问题到时候数据怎么样存储 通过正则表达式来判断吗 看来好像只能这样了
        //还是一个页面一个页面先加着先
        foreach($pageConfig as $key => $val ){
            if($val == 'text'){
                $form.='<input type="text" value="" name="'.$key.'" id="'.$key.'" class="form-control" /> &nbsp;&nbsp;'.$configInfo[$pagetype][$key].'<br /><br />';
            }else if($val== 'img'){
                $form .= $configInfo[$pagetype][$key].'<br />';
                $form .= '<input type="text" id="'.$key.'" name="banner" class="form-control" style="width:550px;"><script src="/tpl/static/upyun.js?2013"></script>';
                $form .='&nbsp;<a href="javascript:void(0)" onclick="upyunPicUpload(\''.$key.'\',700,420,\''.$key.'\')" class="a_upload">上传</a>&nbsp;<a href="#" onclick="viewImg(\''.$key.'\')" class="a_upload">预览</a><br /><br />';
            }
        }
        $form .= '';
        $info['form'] = $form;
        $info['pageConfig'] = $pageConfig;
        $info = json_encode($info);
        exit($info);
    }

    public function setPage(){

        $pagearg = htmlspecialchars_decode($_POST['pagearg']);
        $pagearg = json_decode($pagearg,true);

        $token = $_GET['token'];
        $tplname = $_GET['tplname'];
        $name = $_GET['showname'];//功能库调用显示用
        $music = $_POST['music'];
        $pic = $_POST['backpic'];

        $data = array();
        $data['tplname'] = $tplname;
        $data['name'] = $name;
        $data['token'] = $token;
        $data['arg'] = serialize($pagearg);
        if($music) $data['music'] = $music;
        if($pic) $data['pic'] = $pic;

        $m = M('Wjbinvite_data')->add($data);

        exit("添加成功");
    }

    /**
     * 查看报名的用户
     */
    public function lookuser(){
        $id=$_GET['id'];

        $token = $_GET['token'];

        $where = array('yid'=>$id,'token'=>$token);

        $user  = M('Wjbinvite_user')->where($where)->select();

        $this->assign('user',$user);

        $this->display();
    }

    /**
     * 编辑场景 但是 模板主题不能再换了
     */
    public function edit(){

        $id= $_GET['id'];

        $token = $_GET['token'];

        $modelData = M('Wjbinvite_data')->where(array('id'=>$id,'token'=>$token))->find();

        $model = M('Wjbinvite_model')->where(array('tplname'=>$modelData['tplname']))->find();

        $model['config'] = unserialize($model['config']);

        $model['configinfo'] = unserialize($model['configinfo']);

        $modelData['arg'] = unserialize($modelData['arg']);

        $this->assign('modelData',$modelData);

        $this->assign('model',$model);

        //var_dump($model);exit;
        $this->display();

    }


    public function proedit(){

        $token = $_GET['token'];

        $id = $_GET['id'];

        $music = isset($_POST['music'])?$_POST['music']:'';

        $data = $_POST['data'];

        $data = htmlspecialchars_decode($data);

        $data = json_decode($data,true);

        $data = serialize($data);

        $theData = array();

        $theData['arg'] = $data;

        $theData['music'] = $music;

        M('Wjbinvite_data')->where(array('token'=>$token,'id'=>$id))->save( $theData );

        $this->success("修改成功");

    }


    /**
     * 删除某个一场景邀请
     * 连同报名的用户也要删除
     */
    public function delete(){

        $id = isset($_GET['id'])?$_GET['id']:0;

        $token = isset($_GET['token'])?$_GET['token']:0;

        if(!$id || !$token) $this->error("操作失败");

        M('wjbinvite_data')->where(array('id'=>$id,'token'=>$token))->delete();

        M('wjbinvite_user')->where(array('yid'=>$id,'token'=>$token))->delete();

        $this->success("操作成功");

    }
}





